﻿#region Using Statements
using DirectZZT.Shared.Core;
using DirectZZT.Shared.World.Objects;
using DirectZZT.Shared.World;
#endregion

namespace DirectZZT.BoardObjects
{
    /// <summary>
    /// Represents the ammo ("Ammo") board object.
    /// </summary>
    [Export("Ammo")]
    [BoardObject(Identifier = "Ammo", Classification = BoardObjectClassification.Item, AllowBecome = false)]
    public sealed class AmmoBoardObject : BoardObject, IItemBoardObject
    {
        #region Constants

        /// <summary>
        /// Defines the amount of ammo this item adds.
        /// </summary>
        private const int AmmoAmount = 5;

        #endregion

        #region Fields

        private int _incrementAmount = AmmoAmount;

        #endregion

        #region Properties

        /// <summary>
        /// Ammo can be moved and has collision.
        /// </summary>
        public override BoardObjectCapabilities Capabilities
        {
            get { return base.Capabilities | BoardObjectCapabilities.Movable; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the AmmoBoardObject class.
        /// </summary>
        public AmmoBoardObject()
        {
            this.Texture = "Textures/PlayfieldObjects/Ammo";
            this.Enabled = true;
            this.Visible = true;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Not even in DirectZZT supported?
        /// </summary>
        /// <param name="featureSet"></param>
        /// <returns></returns>
        protected override bool CanAttachScript(FeatureSet featureSet)
        {
            return false;
        }

        /// <summary>
        /// Called when this object spawns.
        /// </summary>
        /// <param name="modifiers"></param>
        public override void OnSpawn(string[] modifiers)
        {
            base.OnSpawn(modifiers);

            // read out modifiers, if allowed for this item
            if (this.Board.World.FeatureSet == FeatureSet.DirectZZT && (modifiers != null && modifiers.Length == 1))
            {
                _incrementAmount = modifiers[0].TryGetInt32(AmmoAmount);
                // no negative values!
                if (_incrementAmount < 0)
                {
                    _incrementAmount = AmmoAmount;
                }
            }
        }

        #endregion

        #region IItemBoardObject Members

        bool IItemBoardObject.CanPickup(BoardObject obj)
        {
            // only a player can pick us up
            return obj is IPlayerBoardObject;
        }

        void IItemBoardObject.OnPickup(BoardObject obj)
        {
            IPlayerBoardObject player = (IPlayerBoardObject)obj;
            // add ammo
            player.Player.Inventory.AmmoAmount += _incrementAmount;
        }

        #endregion
    }
}
